package com.athena.api.controller;

import com.athena.api.model.UrlMethodInfo;
import com.athena.api.model.UrlParamInfo;
import com.athena.framework.annotation.Controller;
import com.athena.framework.annotation.ResponseBody;
import com.athena.framework.annotation.UrlMapping;
import com.athena.framework.url.info.UrlInfo;
import com.athena.framework.url.info.UrlInfoFactory;
import com.athena.util.MethodUtil;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Controller(desc = "获取系统运行时的所有url接口信息说明")
@UrlMapping("/api")
public class APIController {

    private UrlInfoFactory urlInfoFactory = UrlInfoFactory.creatUrlInfoFactory();

    @UrlMapping(value = "/index", desc = "返回api说明页面的html文件")
    public String apiPage() {
        return "/api/index.html";
    }


    @UrlMapping(value = "/getAllController", desc = "获取系统运行时所有的controller")
    @ResponseBody
    public List<UrlInfo> getAllController() {
        List<UrlInfo> urlList = new LinkedList<>();
        urlInfoFactory.forEachUrlMap((key, urlInfo) -> {
            if ("class".equals(urlInfo.getType())) {
                urlList.add(urlInfo);
            }
        });
        return urlList;
    }

    @UrlMapping(value = "/getUrlInfo", desc = "获取URL的详细信息", parm = {"url路径的key"})
    @ResponseBody
    public List<UrlMethodInfo> getUrlInfo(String urlKey) {
        List<UrlMethodInfo> list = new LinkedList<>();
        UrlInfo controllerInfo = urlInfoFactory.getUrlInfoByKey(urlKey);
        if (controllerInfo != null) {
            urlInfoFactory.forEachUrlMap((key, info) -> {
                if (info.getParent() != null && info.getParent().getClass() == controllerInfo.getExecutor().getClass()) {
                    UrlMethodInfo urlMethodInfo = new UrlMethodInfo();
                    urlMethodInfo.setTitle(info.getUrl());
                    urlMethodInfo.setDescription(info.getDesc());
                    //获取参数信息
                    Method method = (Method) info.getExecutor();
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    if (parameterTypes == null || parameterTypes.length == 0) {
                        urlMethodInfo.setParmData(null);
                    } else {
                        List<UrlParamInfo> paramInfoList = new ArrayList<>();
                        String[] paramNames = MethodUtil.parameterName(method);
                        UrlMapping annotation = method.getAnnotation(UrlMapping.class);
                        String[] parm = annotation.parm();
                        for (int i = 0; i < parameterTypes.length; i++) {
                            UrlParamInfo paramInfo = new UrlParamInfo();
                            paramInfo.setName(paramNames[i]);
                            paramInfo.setType(parameterTypes[i].getName());
                            paramInfo.setParamDesc(parm.length >= i + 1 ? parm[i] : "");
                            paramInfoList.add(paramInfo);
                        }
                        urlMethodInfo.setParmData(paramInfoList);
                    }
                    list.add(urlMethodInfo);
                }
            });
        }
        return list;
    }
}
